#pragma once

#include <compl.h>

STA_OUFUN(lknode)
void * (* const get_lknode)(const size_t size);
void (* const ret_lknode)(void *lknode, const size_t size);
END_OUFUN(lknode)

/* 定义alocknode, 这些应该挪到平台依赖中 */
#include <dinfra/spslab.h>
#define DEF_LOCKNODE_SPSLAB(objname, ptlscache, objsize, slabsize)					\
DEF_SPSLAB_OBJ_STAIC_WITH_TLSCACHE(objname, ptlscache, objsize, slabsize)			\
SAI void *objname##_get_locknode(const size_t size){ if (size <= objsize) return (void *)objname##_cachealloc(); return NULL; }\
SAI void objname##_ret_locknode(void *locknode, const size_t size){ PARAM_USED(size); objname##_cachefree((spobj_t*)locknode); }\
NEW_OUFUN(lknode, spslablknode, objname##_get_locknode, objname##_ret_locknode)

#define DEF_LOCKNODE_MALLOC(objname, objsize)								\
SAI void * objname##_get_locknode(const size_t size){							\
	void *ret = NULL;										\
	if (size <= objsize) ret = __builtin_malloc(objsize); 						\
	ASSUME(ret);											\
	return ret; }											\
SAI void objname##_ret_locknode(void *locknode, const size_t size){ __builtin_free(locknode); }		\
NEW_OUFUN(lknode, malloclknode, objname##_get_locknode, objname##_ret_locknode)

// NEW_CEFF_HDL(lknode, malloclknode, get_locknode, ret_locknode)

#define DEF_LOCKNODE_ALIGNMALLOC(objname, objsize)							\
SAI void * objname##_get_locknode(const size_t size){							\
	void *ret = NULL;										\
	ret = aligned_alloc(HDEF_CACHE_LINE_BYTES, size);						\
	ASSUME(ret);											\
	return ret; }											\
SAI void objname##_ret_locknode(void *locknode, const size_t size){ PARAM_USED(size); free(locknode); }	\
NEW_OUFUN(lknode, alignlknode, objname##_get_locknode, objname##_ret_locknode)

#define DEF_LOCKNODE_CUSTOM(objname, objsize, pcachealloc, pcachefree)					\
SAI void *objname##_get_locknode(const size_t size){ if (size <= objsize) return (void *)pcachealloc (); return NULL; }\
SAI void objname##_ret_locknode(void *locknode, const size_t size){ PARAM_USED(size); pcachefree (locknode); }\
NEW_OUFUN(lknode, objname, objname##_get_locknode, objname##_ret_locknode)
